package com.polaris.live.popup

import android.content.Context
import android.view.LayoutInflater
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.databinding.LayoutPopupBottomButtonBinding
import com.polaris.live.databinding.PopupBottomButtonBinding

/**
 * 自定义button的弹窗
 *
 * @author Created by lucas on 2023/10/25 16:26
 * @since 1.0.0
 */
class BottomButtonPopup(
    context: Context,
    private val mTitle: String,
    private val mButtons: List<ButtonInfo>,
    private val mNegativeOnClickListener: PopupOnClickListener?
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupBottomButtonBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_bottom_button
    }

    override fun onCreate() {
        super.onCreate()

        mBinding = PopupBottomButtonBinding.bind(popupImplView)

        mButtons.forEachIndexed { index, info ->
            val childBinding = LayoutPopupBottomButtonBinding.inflate(
                LayoutInflater.from(context),
                mBinding.llContent,
                false
            )

            childBinding.button.text = info.text
            childBinding.button.setOnClickListener {
                info.onClickListener.onClick(this)
            }

            if (index == mButtons.size - 1) {
                childBinding.line.visibility = GONE
            }

            mBinding.llContent.addView(childBinding.root)
        }

        mBinding.tvTitle.text = mTitle

        val negativeOnClickListener = mNegativeOnClickListener
        if (negativeOnClickListener == null) {
            mBinding.vLine.visibility = GONE
            mBinding.tvCancel.visibility = GONE
        } else {
            mBinding.tvCancel.setOnClickListener {
                negativeOnClickListener.onClick(this)
            }
        }

        mBinding.ivClose.setOnClickListener {
            dismiss()
        }

        NavigationBarUtils.setNavigationHeightIfTransparent(mBinding.tvCancel)
    }

    class Builder(
        private val context: Context,
        private val title: String
    ) {

        private val buttons = mutableListOf<ButtonInfo>()

        private var negativeOnClickListener: PopupOnClickListener? = null

        fun addButton(text: String, onClickListener: PopupOnClickListener): Builder {
            buttons.add(ButtonInfo(text, onClickListener))
            return this
        }

        fun setNegativeListener(l: PopupOnClickListener): Builder {
            negativeOnClickListener = l
            return this
        }

        fun build(): BottomButtonPopup {
            return BottomButtonPopup(context, title, buttons, negativeOnClickListener)
        }
    }

    data class ButtonInfo(
        val text: String,
        val onClickListener: PopupOnClickListener
    )
}

fun interface PopupOnClickListener {

    fun onClick(popup: BasePopupView)

}